package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 根据分类的id，查询商品分类
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        return ResponseEntity.ok(new CategoryDTO(category));//把Entity转为DTO
    }

    /**
     * 根据分类id的集合，查询商品分类的集合
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        List<Category> categoryList = categoryService.listByIds(ids);
        return ResponseEntity.ok(
                CategoryDTO.convertEntityList(categoryList) // 把Entity集合转为DTO集合
        );
    }

    /**
     * 根据品牌id查询分类集合
     * @param brandId 品牌id
     * @return 分类集合
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long brandId){
        return ResponseEntity.ok(categoryService.queryCategoryByBrandId(brandId));
    }

    /**
     * 根据父类目id，查询子类目的集合
     * @param pid 父类目的id
     * @return 子类目的集合
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        List<Category> list = categoryService.query().eq("parent_id", pid).list();//// 根据父类目id查询集合
        return ResponseEntity.ok(
                CategoryDTO.convertEntityList(list)// 把PO集合转为DTO集合
        );
    }
}

